[deps]
gmp
mpfr
mpc
zlib

[deps.host]
m4
zlib
libelf


[mirrors]
http://ftpmirror.gnu.org/gcc/gcc-4.7.4/gcc-4.7.4.tar.bz2

[vars]
filesize=82935453
sha512=dfcb737073191e628231031a3571ec77ee760a59377630f4a6e4fdfa66f9ddad39fde47e3f0f227eb43cdf90e0d34cde5abdc9ac892c1e111a911062a66c9189

[build]
VERSION=4.7.4
VERSION_REMOVE='s,4\.7\.4.*,,'
# check that libc.so is available, otherwise we'll get a libstdc++.so
# that is statically linked against musls libc.a.
# this will lead to random crashes in C++ apps since two concurrent
# malloc implementations will call brk and mess up each other.
if [ ! -f "$butch_prefix"/lib/libc.so ] ; then
	echo "libc.so non-existing or symlink broken"
	exit 1
fi

patch -p1 < "$K"/gcc473-unwind-dliterate.patch
patch -p1 < "$K"/gcc473-dliterate-configure-var.patch
patch -p1 < "$K"/gcc473-arm-hwcap.patch
patch -p1 < "$K"/gcc473-config-sub.patch
patch -p1 < "$K"/gcc473-powerpc-libc-stack-end.patch
patch -p1 < "$K"/gcc-474-universal-initializer-no-warning.patch
patch -p1 < "$K"/gcc474-libssp_nonshared.patch
patch -p1 < "$K"/gcc474-libgcc_fixver.patch

# fix specs file generation. this prevents libgcc_s from being used.
# not only is it slower, it'll cause assertion errors in ld.
sed -i 's,-lgcc_s,--start-group -lgcc_eh -lgcc -lc --end-group,' gcc/gcc.c

# we're doing a bit of hack to place the g++ contents in this folder
# the build system isnt really laid out for extracting multiple tarballs into
# the same dir...

libibertyfix libiberty

#more g++ hackery
#sed -i 's,# include <syscall.h>,#include <sys/syscall.h>,' ./libstdc++-v3/libsupc++/guard.cc

#todo check if this isnt to intrusive
sed -i 's,gcc_no_link=yes,gcc_no_link=no,' ./libstdc++-v3/configure

#and even more g++ hackery
mv ./libstdc\+\+-v3/config/os/gnu-linux ./libstdc\+\+-v3/config/os/gnu-linux.org
cp -r ./libstdc\+\+-v3/config/os/generic ./libstdc\+\+-v3/config/os/gnu-linux
cp ./libstdc++-v3/config/os/gnu-linux.org/arm-eabi-extra.ver ./libstdc++-v3/config/os/gnu-linux/
#g++ hackery done

mv ./libitm/config/linux/x86 ./libitm/config/linux/x86_glibc
cp -r ./libitm/config/generic ./libitm/config/linux/x86

sed -i 's@\./fixinc\.sh@-c true@' gcc/Makefile.in

sed -i 's@/lib/ld-linux.so.2@/lib/ld-musl-i386.so.1@' gcc/config/i386/linux.h
sed -i 's@/lib/ld-linux.so.2@/lib/ld-musl-i386.so.1@' gcc/config/i386/linux64.h
if [ "$A" = arm ] ; then
dlarch=arm
$CC -dM -E - < /dev/null | grep __ARMEB__ > /dev/null && dlarch=${dlarch}eb
$CC -dM -E - < /dev/null | grep __ARM_PCS_VFP > /dev/null && dlarch=${dlarch}hf
sed -i 's@/lib/ld-linux.so.3@/lib/ld-musl-'$dlarch'.so.1@' gcc/config/arm/linux-eabi.h
sed -i 's@/lib/ld-linux.so.2@/lib/ld-musl-'$dlarch'.so.1@' gcc/config/arm/linux-elf.h
elif [ "$A" = mips ] ; then
dlarch=mips
$CC -dM -E - < /dev/null | grep _MIPSEL > /dev/null && dlarch=${dlarch}el
$CC -dM -E - < /dev/null | grep __mips_soft_float > /dev/null && dlarch=${dlarch}-sf
sed -i 's@/lib/ld.so.1@/lib/ld-musl-'$dlarch'.so.1@' gcc/config/mips/linux.h
sed -i 's@/lib/ld.so.1@/lib/ld-musl-'$dlarch'.so.1@' gcc/config/mips/linux64.h
sed -i 's@/lib64/ld.so.1@/lib/ld-musl-'$dlarch'.so.1@' gcc/config/mips/linux64.h
sed -i 's@/lib32/ld.so.1@/lib/ld-musl-'$dlarch'.so.1@' gcc/config/mips/linux64.h
fi
sed -i 's@/lib64/ld-linux-x86-64.so.2@/lib/ld-musl-x86_64.so.1@' gcc/config/i386/linux64.h
sed -i 's@/lib/ld.so.1@/lib/ld-musl-powerpc.so.1@g' gcc/config/rs6000/sysv4.h
sed -i 's@/lib/ld.so.1@/lib/ld-musl-powerpc.so.1@g' gcc/config/rs6000/linux64.h
sed -i 's@/lib64/ld64.so.1@/lib/ld-musl-powerpc64.so.1@g' gcc/config/rs6000/linux64.h

patch -p1 < "$K"/gcc-453-patch0.patch

config_arch_flags=
config_arch_target_cflags=
config_arch_target_ldflags=
if [ "$A" = "arm" ] ; then
	patch -p1 < "$K"/gcc-453-patch1.patch
elif [ "$A" = "powerpc" ] ; then
	patch -p1 < "$K"/gcc-454-stackend.patch
	config_arch_flags="--enable-secureplt --with-long-double-64"
	config_arch_target_cflags="-Wl,--secure-plt"
	config_arch_target_ldflags="-Wl,--secure-plt"
fi

machine=
if [ -n "$CROSS_COMPILE" ] ; then
	if [ -z "$GCC_ARCH_CONFIG_FLAGS" ] ; then
		#autodetect arch specifics if not passed via config
		gccv=$($CC -v 2>&1|grep "^Configured with: ")
		for flag in --with-arch --with-float --with-fpu ; do
			for arg in $gccv ; do
				printf "%s" "$arg" | grep "^$flag=" >/dev/null \
				&& { GCC_ARCH_CONFIG_FLAGS="$arg $GCC_ARCH_CONFIG_FLAGS" ; break ; }
			done
		done
	fi
	machine=$($CC -dumpmachine)
	xconfflags="--host=$machine --target=$machine $GCC_ARCH_CONFIG_FLAGS"
	export GCC_FOR_TARGET="$CC"
fi
export CC="$CC -static -D_GNU_SOURCE"

CFLAGS="-O0 -s" LDFLAGS="-Wl,--no-keep-memory" \
CFLAGS_FOR_TARGET="$config_arch_target_cflags" \
LDFLAGS_FOR_TARGET="$config_arch_target_ldflags" \
./configure -C --with-headers=no --prefix="$butch_prefix" \
  --disable-multilib \
  --with-multilib-list= \
  --disable-nls \
  --disable-mudflap \
  --disable-libmudflap \
  --enable-libssp \
  --disable-libgomp \
  --disable-debug \
  --disable-bootstrap \
  --disable-gnu-indirect-function \
  --enable-lto \
  --with-system-zlib \
  --with-target-libiberty=no --with-target-zlib=no \
  --enable-languages=c,c++ --enable-clocale=generic \
  $config_arch_flags $xconfflags


gcc_cv_libc_provides_ssp=yes \
make -j$MAKE_THREADS BOOT_LDFLAGS="-static" LDFLAGS="-static"
make -j1 DESTDIR="$butch_install_dir" \
  install-gcc \
  install-lto-plugin \
  install-target-libgcc \
  install-target-libssp \
  install-target-libstdc++-v3

dest=$butch_install_dir$butch_prefix

#create a host_tuple dummy entry, so binutils stuff will get into /bin, etc
[ -z $machine ] && machine=$($dest/bin/gcc -dumpmachine)

[ ! -e ${dest}/${machine} ] && 	( cd $dest ; ln -sf . ${machine} )

#remove gcc's stddef.h which is not compatible
rm $dest/lib/gcc/$machine/$VERSION/include/stddef.h

#remove gcc's libssp.* (we only need libssp_nonshared.a)
rm "$dest"/lib/libssp.*

copy() {
	printf "cp %s %s\n" "$1" "$2"
	cp "$1" "$2"
}

echo link gcc intrinsic headers into /include/gcc
# this allows usage of -I/include/gcc for stuff that uses them
mkdir -p  $dest/include/gcc
for i in $dest/lib/gcc/$machine/$VERSION/include/* ; do
  header=`basename "$i"`
  # do not copy headers that exist in musl
  [ -e "$dest/include/$header" ] || copy "$i" "$dest/include/gcc/" || true
done

ln -sf gcc "$dest"/bin/cc

# assert that libstdc++.so is not statically linked against musl
if ! readelf -a "$dest"/lib/libstdc++.so | grep NEEDED | grep libc.so > /dev/null ; then
	echo "error: libstdc++.so has no NEEDED entry for libc.so!"
	exit 1
fi

ln -sf gcc "$dest"/bin/gcc-$VERSION
ln -sf g++ "$dest"/bin/g++-$VERSION

rm "$butch_install_dir""$butch_prefix"/lib/*.la

